//
//  TravelImageViewController.m
//  旅游咨询
//
//  Created by 徐斌 on 15/4/18.
//  Copyright (c) 2015年 xubin. All rights reserved.
//

#import "TravelImageViewController.h"

@interface TravelImageViewController ()

@end

@implementation TravelImageViewController
//2388594880

//2386645259

- (void)viewWillAppear:(BOOL)animated
{
    NSLog(@"wo xiang zou ");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
//    NSLog(@" arrat %@", self.modelArray);
    self.detailScroll = [[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.detailScroll.backgroundColor = [UIColor blackColor];
    self.detailScroll.contentSize = CGSizeMake(self.view.frame.size.width * self.modelArray.count, self.view.frame.size.height - 64);
    [self.view addSubview:self.detailScroll];
    self.detailScroll.pagingEnabled = YES;
    
    [self.navigationController setNavigationBarHidden:YES];
    
    
    TravelDetaileTableViewCell *cella = [[TravelDetaileTableViewCell alloc]init];
//    self.scorllView = [[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
//    self.scorllView.contentSize = CGSizeMake(self.view.frame.size.width * self.modelArray.count, 200);
    
    self.scorllView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 617, self.detailScroll.frame.size.width, 60)];
    
    self.scorllView.contentSize = CGSizeMake(self.detailScroll.frame.size.width, 60);
    self.scorllView.backgroundColor = [UIColor yellowColor];
    
//    13384419617
    self.anim = YES;
    NSLog(@"wo s photo infgo  %@", [[self.modelArray objectAtIndex:0] photo_info]);
    [self.navigationController setNavigationBarHidden:YES];
//    UIImageView *photoImageView = [[UIImageView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
//    [self.detailScroll addSubview:photoImageView];
//    photoImageView.backgroundColor = [UIColor redColor];
//    [photoImageView release];
    
//    
//    NSURL *url = [NSURL URLWithString:[[self.modelArray objectAtIndex:1] photo]];
//    NSLog(@"wo shi url %@", url);
//    [photoImageView sd_setImageWithURL:url];

    UIButton *retunButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    retunButton.backgroundColor = [UIColor yellowColor];
    [retunButton setBackgroundImage:[UIImage imageNamed:@"add_new_poi_back_btn.png"] forState:UIControlStateNormal];
    [retunButton addTarget:self action:@selector(retunButtonAction) forControlEvents:UIControlEventTouchUpInside];
    retunButton.frame = CGRectMake(10, 30, 30, 30);
    
    self.buttonView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    [self.view addSubview:self.buttonView];
    [self.buttonView addSubview:retunButton];
    
    
    
    // 下面的动画
    
    self.myView = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 200)];
    
    self.myScollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height , self.view.frame.size.width, 150)];
    
    self.myScollView.backgroundColor = [UIColor clearColor];
    self.myScollView.contentOffset = CGPointMake(0, 667);
    self.myScollView.bounces = NO;
    
    self.myView.backgroundColor = [UIColor blueColor];
    
    
    
    
    
    
    
    
    self.txtlabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, self.view.frame.size.width - 20, 200)];
    self.txtlabel.backgroundColor = [UIColor clearColor];
    self.txtlabel.numberOfLines = 0;
    self.txtlabel.font = [UIFont systemFontOfSize:17];
    self.txtlabel.textColor = [UIColor whiteColor];
//    self.txtlabel.textAlignment = UIControlContentVerticalAlignmentFill;
    
    
    
    self.returnView = [[UIView alloc]initWithFrame:CGRectMake(0, -self.view.frame.size.height * (64 / 667), self.view.frame.size.width, self.view.frame.size.height * (64 / 667))];
    
    self.returnView.backgroundColor = [UIColor clearColor];
    
    
    self.labeaaa = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 15, 0, 60, 30)];
    self.labeaaa.font = [UIFont systemFontOfSize:20];
//    self.labeaaa.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height * (64 / 667) / 2);
    
    self.labeaaa.textColor = [UIColor whiteColor];
    self.labeaaa.text = @"相册";
    self.labeaaa.font = [UIFont boldSystemFontOfSize:20.0f];

//    [self.myScollView addSubview:self.txtlabel];
    
    
//    [self.returnView addSubview:self.labeaaa];
    
//    [self.view addSubview:retunButton];
    
    [self.view addSubview:self.myScollView];
    [self.view addSubview:self.returnView];
    
    self.detailScroll.delegate = self;
    
    for (int i = 0; i < self.modelArray.count; i++) {
        
      
        
//         UIImageView *photoImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, [[[[self.modelArray objectAtIndex:i] photo_info] objectForKey:@"h"] floatValue] / ([[[[self.modelArray objectAtIndex:i] photo_info] objectForKey:@"w"]floatValue] / 355))];
        
        if ([[[self.modelArray objectAtIndex:i] photo_info] class] != [NSNull class]) {
//            NSLog(@" %d", i);
        UIImageView *photoImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, self.view.frame.size.height)];
            
         
            
//        [[[self.modelArray objectAtIndex:i] photo_info] isKindOfClass:[NSNull class]]
        photoImageView.userInteractionEnabled = YES;
        photoImageView.backgroundColor = [UIColor redColor];
            photoImageView.tag = 10000 + i;
        [self.detailScroll addSubview:photoImageView];
            
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, self.view.frame.size.width - 10, [[cella class] getHeighContent:[[self.modelArray objectAtIndex:i] text]])];
            self.floata = label.frame.size.height;
            label.backgroundColor = [UIColor clearColor];
            label.textColor = [UIColor whiteColor];
            label.numberOfLines = 0;
            label.text = [[self.modelArray objectAtIndex:i]text];
//            [self.myView addSubview:label];
//            [self.myView addSubview:label];
            
//            UIView *textView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
//            textView.tag = 10000 + i;
//            
//            if (textView.tag == 10000 + i) {
//                [textView addSubview:label];
//            }
            
            UIScrollView *scrolView = [[UIScrollView alloc]initWithFrame:CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, label.frame.size.height)];
            scrolView.backgroundColor = [UIColor blueColor];
            [self.scorllView addSubview:scrolView];
            [scrolView addSubview:label];
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapActio:)];
            
            [photoImageView addGestureRecognizer:tap];
            
            
            
            NSURL *url = [NSURL URLWithString:[[self.modelArray objectAtIndex:i] photo]];
            [photoImageView sd_setImageWithURL:url];
            NSString *w = [[[self.modelArray objectAtIndex:i] photo_info] objectForKey:@"w"];
            NSString *h = [[[self.modelArray objectAtIndex:i] photo_info] objectForKey:@"h"];
            CGRect imageFram = CGRectMake(self.view.frame.size.width * i + 10, 10, 355, 100);
            imageFram.size.height = [h floatValue] / ([w floatValue] / 355);
            photoImageView.frame = imageFram;
//            NSLog(@"heigh = %f", imageFram.size.height);
            [photoImageView release];
            photoImageView.center = CGPointMake(self.view.frame.size.width * i + (self.view.frame.size.width / 2),( self.view.frame.size.height  ) / 2);
        }
        

        
        
        
    }
    self.detailScroll.contentOffset = CGPointMake(self.view.frame.size.width * self.index, 0);
    
    
    
    // Do any additional setup after loading the view.
}

- (void)retunButtonAction
{
//
//    UIViewController *VC = vc;
    [self.navigationController popViewControllerAnimated:YES];
    
}



- (void)tapActio:(UITapGestureRecognizer *)tap
{
    
//        [UIView animateWithDuration:3.0 animations:^{
//    
//            // 块里面写实现动画的逻辑
//            self.myView.frame = CGRectMake(100, 400, 100, 100);
//            self.myView.center = CGPointMake(self.view.frame.size.width / 2 , self.view.frame.size.width - self.myView.frame.size.height / 2);
//            self.myView.backgroundColor = [UIColor redColor];
//            self.myView.alpha = 0.3;
//        }];
//    NSLog(@"aaa");
    
    if (self.anim == YES) {
        NSLog(@"aaa");
        
        
        [UIView beginAnimations:@"Animation" context:@"动画"];
        [UIView setAnimationDuration:0.5];
        self.myView.frame = CGRectMake(0, self.view.frame.size.height - self.myView.frame.size.height, 375, 200);
    
        
        
        // 标题相册
        
        self.returnView.frame = CGRectMake(0, 0, self.view.frame.size.width, 64);
        self.labeaaa.frame = CGRectMake(self.view.frame.size.width / 2 - 15, self.returnView.frame.size.height - 30, 60, 30);
        [self.returnView addSubview:self.labeaaa];

        
        
        
        
        
//        NSLog(@"11111 =  %f", self.scorllView.contentOffset.x / self.view.frame.size.width);
//        NSLog(@"22222 =  %@", [[self.modelArray objectAtIndex:(self.detailScroll.contentOffset.x / self.view.frame.size.width)] text]);
        self.txtlabel.text = [[self.modelArray objectAtIndex:(self.detailScroll.contentOffset.x / self.view.frame.size.width)] text];
        NSLog(@"llll %@", self.txtlabel.text);
//        self.myScollView.contentSize = CGSizeMake(self.view.frame.size.width, [[[DataHandle sharDataBaseHandle] class] getHeighContent:self.textLabel.text]);
        
//        self.myScollView.backgroundColor = [UIColor yellowColor];
        
        CGRect frame = self.myScollView.frame;
        
        frame.origin.y =  self.view.frame.size.height - 100;
        
        self.myScollView.frame = frame;
        
//        [self.myView addSubview:self.txtlabel];
//        [self.myScollView addSubview:self.txtlabel];
        // 偏移量
//        self.myScollView.contentOffset = CGPointMake(0, self.detailScroll.frame.size.height - 100);
//        self.myScollView.contentOffset = CGPointMake(0, 567);
        
        [self.myScollView addSubview:self.txtlabel];
        
        
        self.scorllView.contentOffset = CGPointMake(self.detailScroll.frame.origin.x, 620);
        [UIView commitAnimations];
        
//          (self.scorllView.contentOffset.x / self.view.frame.size.width)
        
        
        
        
        
        [self.view bringSubviewToFront:self.buttonView];
        self.anim = NO;
        return;
    }
//    self.view.frame.size.height * (64 / 667)
    if (self.anim == NO) {
        NSLog(@"bbbb");
        [UIView beginAnimations:@"Animation" context:@"动画"];
        [UIView setAnimationDuration:0.5];
        
        self.myView.frame = CGRectMake(0, self.view.frame.size.height, 375, 200);
        self.scorllView.contentOffset = CGPointMake(self.detailScroll.frame.origin.x, 667);
        self.returnView.frame = CGRectMake(0, -64, self.view.frame.size.width, 64);
        
        CGRect frame = self.myScollView.frame;
        
        frame.origin.y =  self.view.frame.size.height;
        
        self.myScollView.frame = frame;
        
//        [self.myScollView addSubview:self.txtlabel];
        
//        self.myScollView.contentOffset = CGPointMake(0, self.detailScroll.frame.size.height);
        self.myScollView.contentOffset = CGPointMake(0, 667);

        
        [UIView commitAnimations];
        self.anim = YES;
        return;
    }
    
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
{
    if (scrollView == self.detailScroll) {
        
        
//          self.txtlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 100)];
        
        
      
        
        
            self.txtlabel.text = [[self.modelArray objectAtIndex:(self.detailScroll.contentOffset.x / self.view.frame.size.width)] text];
            [self.myView addSubview:self.txtlabel];
        
        
//        self.textLabel.frame = CGRectMake(0, 0, self.view.frame.size.width, [[[DataHandle sharDataBaseHandle] class] getHeighContent:self.txtlabel.text]);
        
        
        
        CGRect fram = self.txtlabel.frame;
        fram.size.height = [[[DataHandle sharDataBaseHandle] class] getHeighContent:self.txtlabel.text];
//        fram = self.myScollView.frame;
        self.txtlabel.frame = fram;
        
        
        
        
        
        [self.myScollView addSubview:self.txtlabel];
       
        
        
        self.myScollView.contentSize = CGSizeMake(self.view.frame.size.width, [[[DataHandle sharDataBaseHandle] class] getHeighContent:self.txtlabel.text] + 10);
        
       
        
        self.scorllView.contentOffset = CGPointMake(self.detailScroll.contentOffset.x, self.detailScroll.frame.size.height - 60);
        
//        NSLog(@" %f", self.detailScroll.contentOffset.x);
//        NSLog(@"self detail = %f", self.detailScroll.contentOffset.x);
//        NSLog(@" self  scollv = %f", self.scorllView.frame.origin.x);
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
